/*
 * TransparentScreen.java
 *
 * Created on 24 Feb, 2010;
 */

package com.ahmad.fvautoclicker.v2;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;

/**
 * 
 * @author Ahmad Nadeem<br>
 *         <a mailto:"ahmad.nk@gmail.com">
 */
public class TransparentScreen extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** Creates new form TransparentScreen */
	public TransparentScreen(MainFrame mainFrame) {
		this.frame = mainFrame;
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		updateBackground();
		initComponents();
		this.setSize(d);
	}

	private Image background;
	private MainFrame frame;

	public void updateBackground() {
		try {
			Robot rbt = new Robot();
			Toolkit tk = Toolkit.getDefaultToolkit();
			Dimension dim = tk.getScreenSize();
			background = rbt.createScreenCapture(new Rectangle(0, 0, (int) dim
					.getWidth(), (int) dim.getHeight()));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void paintComponent(Graphics g) {
		Point pos = this.getLocationOnScreen();
		Point offset = new Point(-pos.x, -pos.y);
		g.drawImage(background, offset.x, offset.y, null);
	}

	// private JFrame frame;

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		setLayout(null);
		pnl = new ControlPanel(this.frame);
		this.add(pnl);
		pnl.setBounds(0, 0, pnl.width, pnl.height);

	}// </editor-fold>

	// GEN-END:initComponents

	public void refresh() {
		try {
			Thread.sleep(700);
			updateBackground();
			repaint();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

	// GEN-BEGIN:variables
	// Variables declaration - do not modify
	public ControlPanel pnl;

	// End of variables declaration//GEN-END:variables

}